Module# 02: Generic Programming                                          Lecture#03: Generic Class Basics

 

// Example 3.1: Program to handle an array of integers

 

class SpecificArrayInt {

     // Declaring an array of integer numbers

     int a;

 

     // Constructor to load the array

     SpecificArrayInt(int a[]) {

         this.a = a;

     } 

 

     // Method to print the array elements

     void printInt() {

         for(int x : a)

             System.out.println(x);

     }

 

// Method to reverse the array elements

   void reverseInt() {

        j = a.length;

        for (int i=0; i<j; i++)

           int temp;

           temp = a[i];

           a[i] = a[j];

           a[j] = temp;

           j--;

       } // End of for-loop

    } // end of method

} // end of class

 

class MainClassInt {

      //This class use the class SpecificArrayInt to manipulate data in it

      SpecificArrayInt a = {1, 2, 3, 4, 5};

      a.printInt();

      a.reverseInt();

      a.printInt();

}

 

// Example 3.2: Program to handle an array of doubles

 

class SpecificArrayDouble {

     // Declaring an array of double values

     double b;

 

     // Constructor to load the array

     SpecificArrayDouble(double b[]) {

         this.b = b;

     } 

 

     // Method to print the array elements

     void printDouble() {

         for(double x : b)

             System.out.println(x);

     }

 

// Method to reverse the array elements

   void reverseDouble() {

        j = b.length;

        for (int i=0; i<j; i++)

           double temp;

           temp = a[i];

           a[i] = a[j];

           a[j] = temp;

           j--;

       } // End of for-loop

    } // end of method

} // end of class

 

class MainClassDouble {

      //This class use the class SpecificArrayInt to manipulate data in it

      SpecificArrayDouble b = {1.2, 2.3, 3.4, 4.5, 5.6};

      b.printDouble();

      b.reverseDouble();

      b.printDouble();

}

 

// Example 3.3: Program to handle an array of Strings

 

class SpecificArrayString {

     // Declaring an array of double values

     String c;

 

     // Constructor to load the array

     SpecificArrayDouble(String c[]) {

         this.c = c;

     } 

 

     // Method to print the array elements

     void printString() {

         for(String x : c)

             System.out.println(x);

     }

 

// Method to reverse the array elements

   void reverseString() {

        j = c.length;

        for (int i=0; i<j; i++)

           double temp;

           temp = c[i];

           c[i] = c[j];

           c[j] = temp;

           j--;

       } // End of for-loop

    } // end of method

} // end of class

 

class MainClassString {

      //This class use the class SpecificArrayInt to manipulate data in it

      SpecificArrayDouble b = {“A”, “B”, “C”, “D”, “E”};

      c.printString();

      c.reverseString();

      c.printString();

}

 

// Example 3.4: Defining a generic class

 

public class GeneriClass <T> {

       // Two elemnts of generic type T is defined below

       private T x;      

      

       // Constructor

       public GenericClass(T t) {

        x = t;

       }

       // Print the T-type value for an object

       public void printData() {

        System.out.println (x);

       }

  }  // This completes the definition of a simple generic class GeneriClass<T>

 

// The driver class is defined below, which creates different types of arrays.

class GenericClassTest {

     public static void main( String args[] ) { 

      // A data with the member as String

      GenericClass<String> a = new GenericClass<String> ("Java");

      a.printData();

      // A data with the member as integer value

      GenericClass<Integer> b = new GenericClass<Integer> (123);

      b.printData();

      // A data with the member as float value

      GenericClass<Double> c = new GenericClass<Double> (3.142);

      c.printData();

    }

}

 

// Example 3.5: Defining a generic class to process arrays with any data types

 

class GenericArray<T> {

   //Declaring an array, which should store any type T of data

   T a[ ];    

 

   GenericArray(T x) {        // Define a constructor

      a = x;

   }

 

   T getData(int i) {    // To return the element stored in the i-th place in the array

       return a[i];

   }

  

   void static printData (T b) {  // A generic method to print the elements in array b

      for(int i = 0; i < b.length(); i ++)

          System.out.print(b.getData(i) +  ); // Print the i-th element in b

      System.out.println();      // Print a new line

   }

 

// Generic method to reversed the order of elements in array b

void static reverseArray (T b) {

        int front = 0, rear = b.length-1;

        T temp;

        while( front < rear)  {

               temp = b[rear];

             b[rear] = a[front];

             a[front] = temp;

             front++; rear--;

        }

    }

}